﻿using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class PixelBlurMask : MonoBehaviour 
{

	public Shader				ShaderLink = null;
	public float				BlurStrength = 1.0f;

	Material					mMtrl = null;

	Material material
	{
		get
		{
			if( this.mMtrl == null )
			{
				this.mMtrl = new Material( this.ShaderLink );
				this.mMtrl.hideFlags = HideFlags.HideAndDontSave;
			}
			return this.mMtrl;
		}
	}

	void OnEnable()
	{
		if( SystemInfo.supportsImageEffects == false )
		{
			this.enabled = false;
			return;
		}
		if( ( this.ShaderLink == null ) ||
			( this.ShaderLink.isSupported == false ) )
		{
			this.enabled = false;
			return;
		}
	}
	void OnDisable()
	{
		if( this.mMtrl != null )
		{
			Material.DestroyImmediate( this.mMtrl );
		}
	}


	void Start()
	{
		this.material.SetFloat( "_BlurStrength", this.BlurStrength );
	}

	void OnRenderImage( RenderTexture src, RenderTexture dest )
	{
		this.material.SetFloat( "_BlurStrength", this.BlurStrength );
		Graphics.Blit( src, dest, this.material );
	}


}
